package paskal.ui.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.Map;

import javax.swing.JTextField;

import paskal.ui.Style;

public class TextField extends JTextField implements Component {
	private static final long serialVersionUID = 113822395935627109L;
	private Color borderColor = null;

	
	public TextField() {
		setOpaque(false);
		setMargin(new Insets(0, 30, 0, 70));
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		// super.paintBorder(g);
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, getWidth()-1, getHeight()-1);
		g.setColor(borderColor);
		g.drawRect(1, 1, getWidth()-3, getHeight()-3);
	}

	@Override
	public void setProperty(String key, Object value) {
		switch (key) {
		case "bounds": {
			setBounds((Rectangle) value);
			break;
		}
		case "color": {
			Color color = new Color(Integer.parseInt(value.toString()
					.substring(1), 16));
			setForeground(color);
			setCaretColor(color);
			break;
		}
		case "border": {
			@SuppressWarnings("unchecked")
			Map<String, Object> border = (Map<String, Object>) value;
			borderColor = new Color(Integer.parseInt(border.get("color")
					.toString().substring(1), 16));
			break;
		}
		case "background": {
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) value;
			Color color = new Color(Integer.parseInt(background.get("color")
					.toString().substring(1), 16));
			setBackground(color);
			break;
		}
		case "font": {
			@SuppressWarnings("unchecked")
			Map<String, Object> font = (Map<String, Object>) value;
			Font f = new Font(font.get("name").toString(),
					(int) font.get("style"), (int) font.get("size"));
			setFont(f);
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void stylize(String selector) {
		Style.getInstance().styleThisShit(selector, this);
	}

}